
ISO_ROOT = iso_root
LABEL = GrubCD
ISONAME = grubiso
_g2prefix=grub
g2prefix=/$(_g2prefix)
cdboot=$(_g2prefix)/g2ldr
efiboottmpdir=$(ISO_ROOT)
efiboot=boot/efiboot.img
macboot=boot/macboot.img
embed_img=$(PWD)/embed.img
mkisofs=xorriso -report_about HINT -as mkisofs -iso-level 3 -graft-points  -partition_offset 16 -isohybrid-mbr /usr/lib/syslinux/isohdppx.bin
mkisofs=genisoimage
mkisofs_opts=-cache-inodes	\
		-b isolinux/isolinux.bin	\
		-c isolinux/boot.cat \
		-no-emul-boot 		\
		-boot-info-table 		\
		-boot-load-size 4 	\
		$(NULL)


bios_bins=vbios.bin int10.bin

all:$(ISONAME).iso

#efi_bios_bin:$(bios_bins)

iso_dep= exclude.list 		\
		$(ISO_ROOT)/menu.lst \
		$(ISO_ROOT)$(g2prefix)/core.img  \
		$(ISO_ROOT)$(g2prefix)/g2ldr 	\
		$(ISO_ROOT)/$(efiboot)			\
		Makefile 						\
		$(ISO_ROOT)/$(macboot) \
		$(NULL)

$(ISONAME).iso:$(iso_dep) copy_bin isolinux
	/bin/bash -c "cp grub4dos/{*.com,*exe,grldr*}  $(ISO_ROOT)/"
	test -f shellx64.efi && cp shellx64.efi $(ISO_ROOT)/
	test -d maxDOS  && cp -a maxDOS/* $(ISO_ROOT)/
	cp grub2_cfg.embed $(ISO_ROOT)$(g2prefix)/grub.cfg
	./strip_grub_ignore_cd $(ISO_ROOT)/grub.exe
	for f in linux ldlinux config disk elf syslinux reboot menu;do f=/usr/lib/syslinux/modules/bios/$$f.c32;[ -f "$$f" ] || continue;cp -va $$f $(ISO_ROOT)/isolinux;done
	$(mkisofs) -J -r		\
	  $(mkisofs_opts)			\
	  -eltorito-alt-boot		\
	  -e	$(efiboot)	\
	  -no-emul-boot 		\
	  -eltorito-alt-boot		\
	  -e	$(macboot)	\
	  -no-emul-boot 		\
	  -full-iso9660-filenames \
	  -input-charset utf-8 -V $(LABEL) \
	  -exclude-list exclude.list \
	  -o $(ISONAME).iso $(ISO_ROOT)
	isohybrid -u -m $(ISONAME).iso

grub_mod_dir=/usr/lib/grub
pc_target =  i386-pc
pc_mod_dir = $(grub_mod_dir)/$(pc_target)
efi_target=x86_64-efi
efi_mod_dir=$(grub_mod_dir)/$(efi_target)

exclude.list:Makefile
	find $(ISO_ROOT) |grep -E "*~"|sed 's|$(ISO_ROOT)/||g' > $@

g2_mod = echo search normal part_msdos part_gpt fat ntfs ext2 ntfs udf iso9660 video videoinfo video_bochs all_video
#g2_mod=biosdisk iso9660 ext2 fat
####$(shell ls $(mod_dir)/*.mod|sed -e 's|.*/||g;s|\.mod||g')
g2_bios_mod = biosdisk

$(ISO_ROOT)$(g2prefix)/core.img: Makefile grub2_cfg.embed grub2_pc.cfg
	@mkdir -p $(ISO_ROOT)$(g2prefix)
	rm -rf $(ISO_ROOT)$(g2prefix)/grub.cfg
	cp -ar $(pc_mod_dir) /usr/share/grub/*.pf2 $(ISO_ROOT)$(g2prefix) || true
	rm -rf $(ISO_ROOT)$(g2prefix)/grub.cfg
	cp grub2_pc.cfg $(ISO_ROOT)$(g2prefix)/
	/bin/sh -c "cd $(ISO_ROOT)$(g2prefix); \
		grub-mkimage -p $(g2prefix) -O $(pc_target) -o core.img $(g2_mod) $(g2_bios_mod)"

$(ISO_ROOT)$(g2prefix)/g2ldr:$(ISO_ROOT)$(g2prefix)/core.img
	cat $(pc_mod_dir)/cdboot.img $^ > $@

$(ISO_ROOT)/g2ldr.mbr:
	cp $(pc_mod_dir)/$(shell basename $@) $@


isolinux:
	mkdir -p $(ISO_ROOT)/isolinux
	cp /usr/lib/*/isolinux.bin $(ISO_ROOT)/isolinux
	cp isolinux.cfg $(ISO_ROOT)/isolinux

install:all
	test -n "$(bootdev)" || false
	python installgrub $(ISO_ROOT) $(bootdev)
	$(ISO_ROOT)/bootlace.com --time-out=0 --mbr-disable-floppy $(bootdev)
#	grub-install --no-floppy $(bootdev) --boot-directory=$(bootdir)$(g2prefix)

$(efiboottmpdir)/efi/boot/bootx64.efi:grub2_efi.cfg
	mkdir -p $(ISO_ROOT)$(g2prefix)
	cp -ar $(efi_mod_dir) /usr/share/grub/*.pf2 $(ISO_ROOT)$(g2prefix)
	cp grub2_efi.cfg $(ISO_ROOT)$(g2prefix)
	/bin/sh -c "cd $(ISO_ROOT)$(g2prefix); \
		grub-mkimage -p $(g2prefix) --config=grub2_efi.cfg -o grubx64.efi -O $(efi_target) $(g2_mod) "
	mkdir -p $(efiboottmpdir)/efi/boot
	cp $(ISO_ROOT)$(g2prefix)/grubx64.efi $@

$(ISO_ROOT)/$(efiboot):$(efiboottmpdir)/efi/boot/bootx64.efi
	mkdir -p $(ISO_ROOT)/boot
	mformat -C -f 2880 -L 16 -i $(ISO_ROOT)/$(efiboot) ::
	mcopy -s -i $(ISO_ROOT)/$(efiboot) $(efiboottmpdir)/efi ::/

$(embed_img):$(ISO_ROOT)$(g2prefix)/core.img
	cat $(ISO_ROOT)$(g2prefix)/$(pc_target)/boot.img $(ISO_ROOT)$(g2prefix)/core.img > $@

$(ISO_ROOT)/menu.lst:menu.lst
	@mkdir -p $(ISO_ROOT)
	cp $^  $@

$(ISO_ROOT)/$(macboot):Makefile
	mkdir -p macboot $(ISO_ROOT)/boot
	dd if=/dev/zero of=$(ISO_ROOT)/$(macboot).tmp bs=2880K count=1
	mkfs.hfsplus -v $(LABEL) $(ISO_ROOT)/$(macboot).tmp
	sudo /bin/sh -c "umount macboot; mount $(ISO_ROOT)/$(macboot).tmp macboot -o loop,uid=1000,gid=1000"
	mkdir -p macboot/System/Library/CoreServices
	mkdir -p macboot/efi/boot
	cp $(efiboottmpdir)/efi/boot/bootx64.efi macboot/efi/boot
	ln -f macboot/efi/boot/bootx64.efi macboot/System/Library/CoreServices/boot.efi
	./hfs-bless macboot/System/Library/CoreServices/boot.efi || true
	echo "This file is required for booting" > macboot/mach_kernel
	sed "s/PRODUCTVERSION/$(LABEL)/" SystemVersion.plist.in >macboot/System/Library/CoreServices/SystemVersion.plist
	png2icns macboot/.VolumeIcon.icns grubcd_logo.png
	sudo /bin/sh -c "sync;umount macboot"
	mv  $(ISO_ROOT)/$(macboot).tmp $(ISO_ROOT)/$(macboot)

grub2_efi.cfg:grub2_load.cfg.in
	sed "s|@PREFIX@|$(g2prefix)|g" $^ >$@
	echo "set efi_mode=true" >> $@
	echo "export efi_mode" >> $@

grub2_pc.cfg:grub2_load.cfg.in
	sed "s|@PREFIX@|$(g2prefix)|g" $^ >$@

vbios.bin:
	dd if=/dev/mem of=vbios.bin bs=65536 skip=12 count=1

int10.bin:
	dd if=/dev/mem of=int10.bin bs=4 skip=16 count=1

copy_bin:$(bios_bins)
	mkdir -p $(ISO_ROOT)/boot/grub
	cp vbios.bin int10.bin $(ISO_ROOT)/boot
	cp ubuntu_iso_grub.cfg $(ISO_ROOT)/boot/iso_grub.cfg

clean:
	rm -rf *.iso *.list *~ $(ISO_ROOT)/*2.img \
		$(ISO_ROOT)/g2ldr \
		$(ISO_ROOT)$(g2prefix)/g2ldr \
		$(ISO_ROOT)$(g2prefix)/core.img	\
		./$(ISO_ROOT)/*			\
		$(embed_img)			\
		$(efiboottmpdir)/efi	\
		grub2_pc.cfg			\
		grub2_efi.cfg			\
		$(NULL)
